package com.zipeng.controller;

import com.zipeng.entity.R;
import com.zipeng.entity.RegionStats;
import com.zipeng.service.DataService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: zipeng Li
 * 2021/4/27  9:12
 */
@RestController
@RequestMapping("/api")
public class DataController {

    @Resource
    private DataService dataService;

    /**
     * 获取数据带查询
     * @param country
     * @param state
     * @return
     */
    @GetMapping("/search")
    public R searchData(
            @RequestParam(name = "country", required = false)String country,
            @RequestParam(name = "state", required = false)String state){
        List<RegionStats> list = dataService.getList();
        if(list != null){
            if (country != null) {
                list = list.stream().filter(item -> item.getCountry().equals(country)).collect(Collectors.toList());
            }
            if (state != null){
                list = list.stream().filter(item -> item.getState().equals(state)).collect(Collectors.toList());
            }
            return R.success().message("数据查询成功").data("dataList", list);
        }
        return R.error().message("数据获取失败");
    }
}